package csci5828.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import csci5828.search.CityGridSearch;
import csci5828.search.Search;
import csci5828.search.SearchResults;
import csci5828.search.SearchTaskSet;
import csci5828.search.YelpSearch;
import csci5828.tasks.RequestId;
import csci5828.tasks.TaskManager;

public class SearchServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		
		// Get a handle to the global search task manager
		TaskManager<SearchResults> taskManager = TaskManagerUtil.getSearchTaskManager(getServletContext());
		if (taskManager == null) {
			req.setAttribute(RequestAttributes.ERRORS, "Task manager is uninitialized!");
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/pages/error.jsp");
			rd.forward(req, resp);
			return;
		}
		
		// Get the query parameter
		String query = req.getParameter("query");
		if (query == null || query.length() == 0) {
			req.setAttribute(RequestAttributes.ERRORS, "You must provide a query to search for!");
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/pages/error.jsp");
			rd.forward(req, resp);
			return;
		}
		
		// Get the location parameter
		String location = req.getParameter("location");
		if (location == null || location.length() == 0) {
			req.setAttribute(RequestAttributes.ERRORS, "You must provide a location to search for!");
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/pages/error.jsp");
			rd.forward(req, resp);
			return;
		}
		
		// Get the location parameter
		Boolean chkCityGrid = Boolean.valueOf(req.getParameter("CityGrid"));
		Boolean chkYelp = Boolean.valueOf(req.getParameter("Yelp"));
		ArrayList<Search> sources = new ArrayList<Search>();
		//TODO max businesses
		if (!chkCityGrid && !chkYelp) {
			req.setAttribute(RequestAttributes.ERRORS, "You must select a source to search!");
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/pages/error.jsp");
			rd.forward(req, resp);
			return;
		}
		if(chkCityGrid)
		{
			sources.add(new CityGridSearch(location));
		}
		if(chkYelp)
		{
			sources.add(new YelpSearch(location, 3));
		}
		// Create the search task set and submit it
		SearchTaskSet taskSet = 
			new SearchTaskSet(sources.toArray(new Search[sources.size()]), query, 5000, 1000);
		RequestId requestId = taskManager.submit(taskSet);
		
		// Put the generated requestId in the request context
		req.setAttribute("requestId", requestId);
		req.setAttribute("chkCityGrid", chkCityGrid);
		req.setAttribute("chkYelp", chkYelp);
		req.setAttribute("query", 	query);
		req.setAttribute("location", location);
		
		// Forward to the JSP page
		RequestDispatcher rd = getServletContext().getRequestDispatcher("/pages/lpresults.jsp");
		rd.forward(req, resp);
	}

}
